
var superagent = require( 'superagent' );
var superagentCharset = require('superagent-charset'); 
var sha1	   = require( 'sha1' );
var BASEURL	  = 'https://api.cn.rong.io';
var apis = require( '../../cloud/lib/apis' );
var APPKEY 	  = undefined;
var APPSECRET = undefined;
var TIMESTAMP = undefined;
var SIGNATURE = undefined;
var NONCE 	  = undefined;
var HEADERS	  = {};

var FORMAT 	  = 'json';

exports.init = function( appKey, appSecret ) {
	APPKEY 	  = appKey;
	APPSECRET = appSecret;
	NONCE	  = parseInt( Math.random() * 0xffffff );
	TIMESTAMP = Date.parse( new Date() );
	SIGNATURE = sha1( APPSECRET + NONCE + TIMESTAMP  );

	HEADERS['App-Key'] 	 	= APPKEY;
	HEADERS['Nonce'] 		= NONCE;
	HEADERS['Timestamp']	= TIMESTAMP;
	HEADERS['Signature']	= SIGNATURE;

	HEADERS['Content-Type'] = 'application/x-www-form-urlencoded';	
}
exports.kuaidiSearch = function( api, params, callback ) {
	var agent = superagentCharset.agent()
	.post(api);
	for(var k in params) {
		agent=agent.send(k+ "=" + params[k]);
	}
	agent
	.end( function( err,result ) {
		if( err ) {
			return callback( err, null );
		}
		else {
			return callback( null, result );
		}
	} );
}
exports.orderRefund = function( api, params, callback ) {
	
//	for(var k in params) {
//		api+=k+ "=" + params[k]+"&"
//	}
//	api=api.substring(0,api.length-1);
//	 AV.Cloud.httpRequest({
//		  method: 'POST',
//		  url: api,
//		  headers: {
//		    'Content-Type': 'text/html;charset=utf-8'
//		  },
//		  success: function(httpResponse) {
//			  return callback(null,httpResponse.headers.location );
////			  return callback(null,httpResponse.buffer );
//		  },
// error: function(errObj) {
//	 return callback(null);
// }
//});
	
	var myHe={};
	myHe['Content-Type'] = 'application/json';	
	for(var k in params) {
//		agent=agent.send(k+ "=" + params[k]);
		api+=k+ "=" + params[k]+"&"
	}
	api=api.substring(0,api.length-1);
	var agent = superagentCharset.agent()
	.get(api)
	.charset('gbk')
//	.set( myHe );;
	var ps="";
	agent
	.end( function( err,result ) {
		if( err ) {
			return callback( err, null );
		}
		else {
			return callback( null, result );
		}
	} );
}
exports.request = function( api, params, format, callback ) {
	var f = 'json';
	if( (typeof format ) == 'function' ) { // Use default format 'json'
		callback = format;
	}
	else {
		f = format;
	}
	var agent = superagent.agent()
	.post( BASEURL + api + '.' + f )
	.set( HEADERS );
	for(var k in params) {
		agent=agent.send(k+ "=" + params[k]);
	}
	agent.end( function( err,result ) {
		if(api==apis['message']['publish']){
		}
		if( err ) {
			return callback( err, null );
		}
		else {
			return callback( null, result );
		}
	} );
}


exports.requestWithSameFields = function( api, params, fieldAndValues, format, callback ) {
	var f = 'json';
	if( (typeof format ) == 'function' ) { // Use default format 'json'
		callback = format;
	}
	else {
		f = format;
	}
	var agent = superagent.agent()
	.post( BASEURL + api + '.' + f )
	.set( HEADERS );
	
	for( var i=0; i<fieldAndValues.length; ++i ) {
		var field = fieldAndValues[i].field;
		var values = fieldAndValues[i].values;
		for( var j=0; j<values.length; j++ ) {
			agent=agent.send(field+ "=" + values[j]);
		}
	}
	for(var k in params) {
		agent=agent.send(k+ "=" + params[k]);
	}
	agent
	.end( function( err,result ) {
		if( err ) {
			return callback( err, null );
		}
		else {
			return callback( null, result );
		}
	} );
}


exports.validateSignature = function( nonce, timestamp, signature ) {
	return sha1( APPSECRET + nonce + timestamp ) == signature;
}